package service

import (
    . "api/model"
	"errors"
)

func GetCustomerMap() (map[int]*Customer, error) {
	u := Customer{}
    customerRet, err := u.GetCustomer()
	customerMap := map[int]*Customer{}
    if err != nil {
		return customerMap, err
    }

    for _, v := range customerRet{
        customerMap[v.Id] = v
    }

	return customerMap, nil
}

func GetPromotionOfficerMap() (map[int]*PromotionOfficer, error) {
	p := PromotionOfficer{}
    promotionOfficerRet, err := p.GetPromotionOfficer()
	promotionOfficerMap := map[int]*PromotionOfficer{}
    if err != nil {
		return promotionOfficerMap, err
	}

    for _, v := range promotionOfficerRet{
        promotionOfficerMap[v.Id] = v
    }

	return promotionOfficerMap, nil
}

func GetSecuritiesCompanyMap() (map[int]*SecuritiesCompany, error) {
	s := SecuritiesCompany{}
	securitiesCompanyRet, err := s.GetSecuritiesCompany()
	securitiesCompanyMap := map[int]*SecuritiesCompany{}
	if err != nil {
		return securitiesCompanyMap, err
    }

    for _, v := range securitiesCompanyRet{
        securitiesCompanyMap[v.Id] = v
    }

	return securitiesCompanyMap, nil
}

func GetCreditCardMap() (map[int]*CreditCard, error) {
	cd := CreditCard{}
	creditCardRet, err := cd.GetCreditCard()
	creditCardMap := map[int]*CreditCard{}
	if err != nil {
		return creditCardMap, err
	}

    for _, v := range creditCardRet{
        creditCardMap[v.Id] = v
    }

	return creditCardMap, nil
}

func GetCustomerRecordMap() (map[int]*CustomerRecord, error) {
    cr := CustomerRecord{}
	customerRecordRet, err := cr.GetCustomerRecord()
	customerRecordMap := map[int]*CustomerRecord{}
	if err != nil {
		return customerRecordMap, err
	}

    for _, v := range customerRecordRet{
        customerRecordMap[v.Id] = v
    }

	return customerRecordMap, nil
}

func CalPromotionId(crid int, customerMap map[int]*Customer, customerRecordMap map[int]*CustomerRecord) (promotionId int, err error) {
    if _, ok := customerRecordMap[crid]; ok {
        sourceCustomer := customerRecordMap[crid].SourceCustomer
        if _, ok := customerMap[sourceCustomer]; ok {
            return customerMap[sourceCustomer].PromotionId, nil
        } else {
            return 0, nil
        }
    } else {
        return 0, errors.New("crid 不存在")
    }
}

func Settle(crid int, moneyToSelf int, moneyToMaster int, promotionOfficerMap map[int]*PromotionOfficer, customerMap map[int]*Customer, customerRecordMap map[int]*CustomerRecord, associateIds map[int]int) (map[int]*PromotionOfficer, map[int]int, error) {
    promotionId, err := CalPromotionId(crid, customerMap, customerRecordMap)
    if err != nil {
        return promotionOfficerMap, associateIds, err
    }

    // 如果推广官被删除 收入归平台
    if promotionId == 0 {
        return promotionOfficerMap, associateIds, nil
    }

    promotionOfficerMap[promotionId].Income += moneyToSelf
    promotionOfficerMap[promotionId].Withdrawable += moneyToSelf
    associateIds[promotionId] = promotionId

    if promotionOfficerMap[promotionId].Master != 0 {
        masterId := promotionOfficerMap[promotionId].Master
        promotionOfficerMap[masterId].Income += moneyToMaster
        promotionOfficerMap[masterId].Withdrawable += moneyToMaster
        associateIds[masterId] = masterId
    }

    return promotionOfficerMap, associateIds, nil
}

func StructOrder(newOrders []*Order, crid int, phone string, tradingAt string, status int, pId int, pType int, pName string, moneyTotal int, moneyToSelf int, moneyToMaster int, promotionOfficerMap map[int]*PromotionOfficer, customerMap map[int]*Customer, customerRecordMap map[int]*CustomerRecord) []*Order{
    promotionId, err := CalPromotionId(crid, customerMap, customerRecordMap)
    if err != nil {
        return newOrders
    }

    // 如果推广官被删除 收入归平台
    if promotionId == 0 {
        return newOrders
    }

    if promotionId != 0 {
        wd := promotionOfficerMap[promotionId].Withdrawable
        moneyPlatform := moneyTotal - moneyToSelf - moneyToMaster

        newOrders = append(newOrders, &Order{0, phone, tradingAt, status, pId, pType, pName, promotionId, moneyTotal, moneyToSelf, moneyToMaster, moneyPlatform, wd, 0, 0 })
    } else {
        newOrders = append(newOrders, &Order{0, phone, tradingAt, 0, pId, pType, pName, 0, 0, 0, 0, 0, 0, 0, 0 })
    }

    return newOrders
}

func ValidatedDatabase(promotionOfficerMap map[int]*PromotionOfficer) error {
    p := PromotionOfficer{}
    promotionOfficerRet, err := p.GetPromotionOfficer()
    if err != nil {
        return err
    }

    if len(promotionOfficerMap) != len(promotionOfficerRet) {
        return errors.New("系统繁忙，请稍后再试")
    }

    return nil
}